/**
 * Load common libraries.
 * Requires hosting page to include Google's AJAX APIs.
 */
google.load("jquery", "1");
google.load("prototype", "1");
google.load("language", "1");
google.load('search', '1');

/** Application loader. **/
google.setOnLoadCallback(onLoad);

/**
 * Function to be invoked on page load. Sets up the required objects.
 */
function onLoad()
{
	JSON_RPC();
    // Setup JSON-RPC. The whole app depends on this, so...
    JSON_RPC.load(loadSuccess, loadError);
}

/**
 * There was a problem loading JSON-RPC.
 */
function loadError(e)
{
	alert("Really sorry!\nUnable to startup the application.\nMessage reported from the server: " + e.message);
}

/**
 * Contine with the web-app.
 */
function loadSuccess()
{
	// Setup default JQuery AJAX settings. Default 10 seconds timeout. Ensure utf-8 used throughout.
	// Not used now that JSON-RPC is handled by jabsorb.
    //jQuery.ajaxSetup({timeout: 10000, scriptCharset: "utf-8", contentType: "application/json; charset=utf-8"});

    // Setup global parameters and compile classes that are dependent on the google loader.
    compileCoords(this);
    compileRange(this);
    compileContainer(this);
    compilePage(this);
    compileList(this);
    compileNavigationContainer(this);
    compileNavigationController(this);
    compileLocationListPage(this);
    compileMap(this);
    compileMapContainer(this);
    compileMapController(this);
    compileEventsContainer(this);
    compileEventsController(this);
    compileSettingsContainer(this);
    compileSettingsController(this);
    
    Conversions();
    AppInfo();
    UserInfo(userInfoComplete);
}

/**
 * User info setup complete.
 */
function userInfoComplete()
{
	// Setup the messages for the user's language.
	Messages(UserInfo.language, messagesLoaded, messagesFailed);
}

/**
 * Messages loaded.
 */
function messagesLoaded()
{
	// Setup map.
	var mapContainerRange = new Range(new Coords(100, 0), new Coords(AppInfo.viewportWidth-20, AppInfo.viewportHeight-20));
	var locationListPageRange = new Range(new Coords(200,200), new Coords(500,350), true);
	var mapController = new MapController(mapContainerRange, false, 'MapSearchInput', 'MapSearchButton', 'MapContainer', 'DisabledMapContainer', 'MapSearch',
	   'MapCanvas', 'MapTextInfo', locationListPageRange, 'LocationListPage', 'LocationListHead', 'LocationListBody', 'LocationListClose', 17);
	   
	// Setup events.
	var eventsContainerRange = new Range(new Coords(100, 0), new Coords(AppInfo.viewportWidth-20, AppInfo.viewportHeight-20));
	var eventsController = new EventsController(eventsContainerRange, true, 'EventsContainer');
	
	// Setup settings.
	var settingsContainerRange = new Range(new Coords(100, 0), new Coords(AppInfo.viewportWidth-20, AppInfo.viewportHeight-20));
	var settingsController = new SettingsController(settingsContainerRange, true, 'SettingsContainer');
	
	// Setup navigation.
	var navigationContainerRange = new Range(new Coords(10, AppInfo.viewportHeight/2 - 50), new Coords(60, AppInfo.viewportHeight/2 + 50));
	//var navigationContainerRange = new Range(new Coords(10, 200), new Coords(60, 900));
	var navigationController = new NavigationController(navigationContainerRange, 'NavigationContainer', 'mapNav', 'eventsNav', 'settingsNav',
	   mapController, eventsController, settingsController);
}

/**
 * Message loading failed.
 */
function messagesFailed()
{
	// TODO: What to do here?
}